﻿'***********************************************************************
' Assembly         : SOFA.Caching
' Copyright        : (c) Jebarson. All rights reserved.
' Website			: http://www.jebarson.info
'***********************************************************************

Imports SOFA.Caching

Namespace Caching.Test

    ''' <summary>
    ''' List for the countries.
    ''' </summary>
    Public Class CountryLookupList
        Inherits StorageItemCollection(Of CountryLookup)

#Region " Constructors "
        ''' <summary>
        ''' Initializes a new instance of the <see cref="CountryLookupList" /> class.
        ''' </summary>
        Public Sub New()

        End Sub
#End Region

#Region " StorageItem Support "
        ''' <summary>
        ''' Gets the is lazy load.
        ''' </summary>
        ''' <value>true if can be loaded by default or by demand.</value>
        Public Overrides ReadOnly Property IsLazyLoad() As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Loads the specified key.
        ''' </summary>
        ''' <param name="key">The item key.</param>
        ''' <returns>The loaded object.</returns>
        Public Overrides Function Load(ByVal key As String) As Object

            If Me.LoadAll().Where(Function(item) item("CountryId").ToString() = key) IsNot Nothing Then
                Return Me.LoadAll().Where(Function(item) item("CountryId").ToString() = key)(0)
            Else
                Throw New IndexOutOfRangeException(String.Format("Key {0} not found.", key))
            End If
        End Function

        ''' <summary>
        ''' Loads all.
        ''' </summary>
        ''' <returns>The list of loaded objects.</returns>
        Public Overrides Function LoadAll() As System.Collections.Generic.List(Of System.Data.DataRow)
            Dim table As New DataTable
            Dim row As DataRow
            Dim rows() As DataRow
            Dim rowList As New List(Of DataRow)

            With table.Columns
                .Add(New DataColumn("CountryID", GetType(String)))
                .Add(New DataColumn("Name", GetType(String)))
            End With

            With table.Rows
                row = table.NewRow()
                With row
                    .Item(0) = "1"
                    .Item(1) = "India"
                End With
                .Add(row)

                row = table.NewRow()
                With row
                    .Item(0) = "2"
                    .Item(1) = "U.S."
                End With
                .Add(row)

                row = table.NewRow()
                With row
                    .Item(0) = "3"
                    .Item(1) = "Canada"
                End With
                .Add(row)

                ReDim rows(.Count - 1)
            End With

            table.Rows.CopyTo(rows, 0)
            rowList.AddRange(rows)

            Return rowList
        End Function

        ''' <summary>
        ''' Gets the storage config.
        ''' </summary>
        ''' <value>The storage config.</value>
        Public Overrides ReadOnly Property StorageConfig() As ICacheStorageConfig
            Get
                Return CacheConfigManager.GetCacheConfig()
            End Get
        End Property
#End Region

    End Class
End Namespace
